from random import randint

from lib.ZLabel import ZLabel
from lib.ZLoadJson import ZLoadJson
from lib.res.fun import _create_image_alpha
from lib.res.glo import COLOR , enemy_font
from resource.script.Char.lib.Char import Char
from resource.script.Enemy.res.glo import _e_src
from resource.script.Button.lib.EnemyBtn import EnemyBtn


class Enemy ( Char ) :
	def __init__ ( self , father , id , level ) :
		self.data = ZLoadJson ( _e_src [ 'data' ] [ 'enemy' ] [ id ] )
		data = self.data.get ( )
		self.father = father
		data [ 'hp' ] = randint ( int ( self.data.get ( ) [ 'min-hp' ] ) , int ( self.data.get ( ) [ 'hp' ] ) )
		data [ 'ad' ] = randint ( int ( self.data.get ( ) [ 'min-ad' ] ) , int ( self.data.get ( ) [ 'ad' ] ) )
		super ( Enemy , self ).__init__ ( data )
		image = EnemyBtn ( father ,
		                   _create_image_alpha ( self.data.get ( ) [ 'icon-big' ] ) ,
		                   _create_image_alpha ( self.data.get ( ) [ 'icon-big' ] ) ,
		                   self.father._size ,
		                   self )
		image._pos = 0 , 0
		self.level = level
		self.children = [ ]
		self._running = False
		hp_text = ZLabel ( father , str ( self.hp ) , [ 55 , 55 ] , COLOR [ 'font' ] [ 'color' ] [ 3 ] ,
		                   COLOR [ 'font' ] [ 'shadow' ] [ 1 ] , enemy_font )
		ad_text = ZLabel ( father , str ( self.ad ) , [ 5 , 55 ] , COLOR [ 'font' ] [ 'color' ] [ 2 ] ,
		                   COLOR [ 'font' ] [ 'shadow' ] [ 1 ] , enemy_font )
		self.children.append ( image )
		self.children.append ( ad_text )
		self.children.append ( hp_text )

	def draw ( self ) :
		if self._running :
			if self.hp > 0 :
				for x in self.children :
					x.draw ( )
			else :
				self._running = False
				self.father.type = 0
			self.children [ 1 ].draw ( text = str ( self.ad ) )
			self.children [ 2 ].draw ( text = str ( self.hp ) )

	def update ( self , event , pos , key ) :
		if self._running :
			for x in self.children :
				x.update ( event , pos , key )

	def attack ( self ) :
		from resource.ui.sces import players
		players [ 0 ]._hit_hp ( self.ad )
